<template>
  <div class="container">
    <el-card class="app-container">
      <div slot="header" class="clearfix">
        <span>用户管理</span>
      </div>
      <!-- 搜索区域 -->
      <el-form ref="searchForm" label-width="120px" :model="search" class="filter-form">
        <el-row :gutter="5">
          <el-col :xs="24" :sm="12" :md="8" :lg="6">
            <el-form-item label="手机号/昵称:">
              <el-input v-model="search.search" size="small" placeholder="请输入" />
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="8" :lg="6">
            <el-form-item label="状态:">
              <el-select v-model="search.status" size="small" placeholder="请选择">
                <el-option label="正常" value="1" />
                <el-option label="禁用" value="0" />
                <el-option label="冻结" value="2" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :md="8" :lg="12" style="display: flex; justify-content: flex-start;">
            <el-form-item>
              <el-button type="primary" size="medium" @click="handleSearch">查询</el-button>
              <el-button size="medium" @click="resetSearch">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <!-- 数据表格 -->
      <div style="width: 100%;">
        <el-table
          v-loading="tableloading"
          :data="tableData"
          border
          style="width: 100%"
        >
          <el-table-column prop="id" label="ID" width="80" align="center" />
          <el-table-column prop="nickname" label="昵称" align="center" />
          <el-table-column prop="phone" label="手机" align="center" />
          <el-table-column prop="status" label="状态" align="center">
            <template slot-scope="scope">
              {{ scope.row.status === 1 ? '正常' : (scope.row.status === 0 ? '禁用' : '冻结') }}
            </template>
          </el-table-column>
          <el-table-column prop="created_at" label="注册时间" align="center">
            <template slot-scope="scope">
              {{ formatDate(scope.row.created_at) }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="175" align="center">
            <template #default="scope">
              <el-button :loading="loading" :type="scope.row.status === 1 ? 'danger' : 'success'" size="small" @click="handleDisable(scope.row)">{{ scope.row.status === 1 ? '禁用' : '启用' }}</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          :current-page="pagination.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { getUserList, disabledUser } from '@/api/user'
export default {
  data() {
    return {
      search: {
        search: null,
        status: null
      },
      loading: false,
      tableloading: false,
      tableData: [{}],
      pagination: {
        page: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleSearch() {
      this.pagination.page = 1
      this.fetchData()
    },
    resetSearch() {
      this.search = {
        search: null,
        status: null
      }
      this.handleSearch()
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.pagination.page = val
      this.fetchData()
    },
    async handleDisable(row) {
      try {
        this.loading = true
        await disabledUser({ id: row.id })
        await this.fetchData()
        this.loading = false
        this.$message.success('操作成功')
      } catch (error) {
        if (error !== 'cancel') {
          this.$message.error('操作失败')
          console.error('禁用用户失败:', error)
        }
      }
    },
    formatDate(timestamp) {
      if (!timestamp) return '-' // 如果没有时间戳，返回 '-'或其他占位符

      const date = new Date(timestamp)
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      const hours = String(date.getHours()).padStart(2, '0')
      const minutes = String(date.getMinutes()).padStart(2, '0')
      const seconds = String(date.getSeconds()).padStart(2, '0')

      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
    },
    async fetchData() {
      this.tableloading = true
      const queryParams = {
        pageSize: this.pagination.pageSize,
        page: this.pagination.page,
        status: this.search.status,
        search: this.search.search
      }
      const res = await getUserList(queryParams)

      this.tableData = res.data.list
      this.pagination.total = res.data.total
      this.tableloading = false
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-radio__original {
  display: none !important;
}

::v-deep .el-radio:focus:not(.is-focus):not(:active):not(.is-disabled)
  .el-radio__inner {
  box-shadow: none !important;
}
.filter-form {
  padding: 16px;
  padding-bottom: 0;
  margin-bottom: 6px;
  .el-select {
    width: 100%;
  }
}
.tab-container {
  margin-left: 20px;
}
.filter-container {
  margin-bottom: 20px;
}
.filter-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 10px;
}
.filter-row > * {
  flex: 1;
  min-width: 200px;
  max-width: calc(50% - 10px);
}
.filter-actions {
  display: flex;
}
.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 15px;
}
.add {
  ::v-deep .el-dialog__body{
    max-height: 600px;
    overflow-y: auto;
  }
  ::v-deep .el-form-item {
    margin-bottom: 8px;
    .el-form-item__error {
      padding-top: 0px;
      transform: translateY(-2px)
    }
    .el-select {
      width: 100%;
    }
  }
}
.xg {
  ::v-deep .el-dialog__body{
    padding: 20px;
  }
}
.xxzf {
  ::v-deep .el-form-item {
    margin-bottom: 8px;
  }
  ::v-deep .el-form-item__error {
    padding-top: 0px;
    transform: translateY(-2px)
  }
  ::v-deep .el-dialog__body {
    padding: 10px 20px 15px;
  }
}
</style>
